jQuery(function ($) {
    setChartHeight('#hotCityProportionPie');
    initSelectYear(4);
    $('#mostCitySelect').change(function () {
        initMostOrderTotalOfCity();
    });

    initOrderNumberChart();
    initHotCityOrderTotal();
    initMostOrderTotalOfCity();
    initThisLastMostTotal();
});

/**
 * 初始化订单数表格
 */
function drawOrderNumberChart(orderNumberData) {
    let hotCityArr = [],monthArr = [], hotCityData = [];
    for (let g = 0; g < orderNumberData.length; g ++){
        let orderNumberObj = orderNumberData[g], cityName = orderNumberObj.cityName, monthData = [];
        hotCityArr.push(cityName);
        for (let m in orderNumberObj){
            let isMonth = m != 'cityName' && m != 'so_city_code';
            if (isMonth){
                monthData.push(orderNumberObj[m] == null ? 0 : orderNumberObj[m]);
            }
            //添加月份
            if (g == 0 && isMonth){
                monthArr.push(m);
            }
        }
        let onObj = {
            name: cityName,
            type: 'line',
            data: monthData
        }
        hotCityData.push(onObj);
    }
    drawStackedLineChart('热门城市订单数',hotCityArr,monthArr,hotCityData,'orderNumberChart','','单');
}

/**
 * 获取初始化订单数表格数据:获取热门城市近一年的订单数量，对应函数：initOrderNumberChart
 */
function initOrderNumberChart(){
    $.ajax({
        type: "POST",
        url: '/orderCityChart/listHotCityOrderNumber',
        dataType:'json',
        cache: true,
        success: function(orderNumberData){
            drawOrderNumberChart(orderNumberData);
        }
    });
}

/**
 * 初始化热门城市订单总数
 */
function initHotCityOrderTotal(){
    $.ajax({
        type: "POST",
        url: '/orderCityChart/listHotCityOrderTotal',
        dataType:'json',
        cache: true,
        success: function(hotCityTotalData){
            drawCommonPie('热门城市订单总量及占比','订单总量及占比',hotCityTotalData,'hotCityProportionPie');
        }
    });
}

/**
 * 初始化订单最多的6个城市及订单总量
 */
function initMostOrderTotalOfCity(){
    $.ajax({
        type: "POST",
        url: '/orderCityChart/listMostOrderTotalCity?thisYear=' + $('#mostCitySelect').val(),
        dataType:'json',
        cache: true,
        success: function(mostOrderCityData){
            drawMostOrderTotalOfCity(mostOrderCityData);
        }
    });
}

/**
 * 获取配置中series数组里的对象
 * @param seriesName series名称
 * @param seriesDataArr series数据数组
 * @returns series对象
 */
function getSeriesObj(seriesName,seriesDataArr) {
    return {
        name: seriesName,
        type: 'bar',
        barGap: 0,
        label: defaultLabelOption,
        data: seriesDataArr//数据数组
    };
}
/**
 * 画订单最多的6个城市及订单总量柱状图
 * @param mostOrderCityData
 */
function drawMostOrderTotalOfCity(mostOrderCityData) {
    let mocLegendData = [],xAxisArr = [],seriesArr = [];
    let mocObj = mostOrderCityData[0];
    for (let y in mocObj){
        if (y != 'cityName'){
            xAxisArr.push(y.replace('c',''));
        }
    }
    for (let i = 0; i < mostOrderCityData.length; i ++){
        mocObj = mostOrderCityData[i];
        let seriesDataArr = [];
        for (let f in mocObj){
            if (f != 'cityName'){
                seriesDataArr.push(mocObj[f]);
            }
        }
        mocLegendData.push(mocObj.cityName);
        let seriesObj = getSeriesObj(mocObj.cityName,seriesDataArr);
        seriesArr.push(seriesObj);
    }
    let mostTotalOption = getRotationOptionObj('今年订单最多城市与去年对比',mocLegendData,xAxisArr,seriesArr,null,true,false,'单');
    setChartOption(mostTotalOption,'mostOrderTotalOfCity');
}

/**
 * 初始化今年和去年订单量最多的6座城市及其订单量，注意与getMostOrderTotalCity的区别
 * 比如：今年订单量最多的城市，可能是：A,B,C三座城市，则取A,B,C三座城市名称及其今年的订单总量数据，
 *      去年订单量最多的城市，可能是：A,C,D三座城市，则取A,B,C三座城市名称及其去年的订单总量数据。
 */
function initThisLastMostTotal() {
    $.ajax({
        type: "POST",
        url: '/orderCityChart/listMostOrderTotalByYear',
        dataType:'json',
        cache: true,
        success: function(mostData){
            drawThisLastMostTotal(mostData);
        }
    });
}

/**
 * 今年和去年订单量最多的6座城市及其订单量
 * @param mostData 订单数据
 */
function drawThisLastMostTotal(mostData) {
    let thisArr = [], lastArr = [], chartIdArr = ['lastMostTotal','thisMostTotal'],yAxisMax = 0;
    for (let g = 0; g < mostData.length; g ++){
        let tlObj = mostData[g];
        //如果是今年的数据
        if (mostData[g].sort == 1){
            thisArr.push(tlObj);
        } else {
            lastArr.push(tlObj);
        }
        //如果某个统计值 > 坐标轴的最大值
        if (tlObj.yearTotal > yAxisMax){
            yAxisMax = tlObj.yearTotal;
        }
    }
    let r = yAxisMax % 50, d = parseInt(yAxisMax / 50);
    yAxisMax = (r > 0 ? (d + 1) : d) * 50;
    for (let i = 0; i < 2; i ++){
        //如果是今年的数据
        let isLast = i == 0;
        let tlArr = isLast ? lastArr : thisArr, tlLegendArr = [],tlSeriesArr = [],
            tlXAxisArr = [];
        tlXAxisArr.push(tlArr[0].fullYear);
        for (let j = 0; j < tlArr.length; j ++){
            let tlObj = tlArr[j], seriesDataArr = new Array();
            seriesDataArr.push(tlObj.yearTotal);
            if (isLast){
                seriesDataArr.push('');
            }
            tlLegendArr.push(tlObj.cityName);
            let seriesObj = getSeriesObj(tlObj.cityName,seriesDataArr);
            tlSeriesArr.push(seriesObj);
        }
        if (isLast){
            tlXAxisArr.push('');
        }
        let tlTitle = isLast ? '近2年订单最多城市的城市及订单量' : '';
        let tlOption = getRotationOptionObj(tlTitle,tlLegendArr,tlXAxisArr,tlSeriesArr,yAxisMax,isLast,false,'单');
        setChartOption(tlOption,chartIdArr[i]);
    }
}